package Presentacion.Principal;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;
import javax.swing.border.BevelBorder;

import Presentacion.BaseDeDatos.ABMFamilia.ABMFamilia;


/**
 * Muestra el splash screen de la aplicacion mientras carga
 * los componentes del sistema. Ademas muestra una barra de 
 * progreso a medida que carga. Se controla la cantidad de 
 * instancias para por deshabilitarla 
 * @author
 *
 */
@SuppressWarnings("serial")
public class SplashScreen extends JWindow {
	
	public static boolean 	dontShowSplashScreen;
	public static int	  	instances	 = 0;
	private JLabel 			progressText = new JLabel();
	private JProgressBar 	pBar 		 = new JProgressBar(0,100);
	private int 			delay		 = 50;
	
	
	String skin = "c://AppMarket/skin/";  //esta abre de la carpeta skin desde el directorio C:
//	String skin = "src/Presentacion/Img/"; //esta abre de la carpeta IMG interna al proyecto
	
	public static SplashScreen getInstance(){
	
		if( instances == 0 && !dontShowSplashScreen ) 
			return null;
		return new SplashScreen();	
	}
	
	
	
	/**
	 * constructor por default
	 * @author
	 */
	public SplashScreen(){
		
		
		//get the content pane
        JPanel content = (JPanel)getContentPane();
        content.setBackground(Color.white);
        
        
       
        //Set the window's bounds, centering the window
        int width = 403;
        int height = 400;
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (screen.width - width)/2;
        int y = (screen.height - height)/2;
        setBounds( x, y, width, height);
               
        
        //create border
        //BevelBorder border 	= new BevelBorder(BevelBorder.RAISED, Color.YELLOW, Color.red, Color.CYAN, Color.BLUE );
        BevelBorder border 	= new BevelBorder(BevelBorder.RAISED);
        content.setBorder(border);
        content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
        
        //Create a label and progress bar
        JLabel label1 = new JLabel();
        label1.setPreferredSize( new Dimension( 280, 24 ) );
        label1.setAlignmentX(JLabel.CENTER_ALIGNMENT);
        label1.setOpaque(false);
        content.add( label1, BorderLayout.CENTER );
        
          
         Image startImage = Toolkit.getDefaultToolkit().getImage(skin+"inicio.jpg"); 
         label1.setIcon(new ImageIcon(startImage));

                
        //create progress bar       
        this.pBar.setPreferredSize( new Dimension( 300, 7 ) );
        this.pBar.setMaximumSize( new Dimension( 600, 10 ) );
        this.pBar.setMinimum( 0 );
        this.pBar.setMaximum( 100 );
        this.pBar.setValue( 0 );
        this.pBar.setAlignmentX(CENTER_ALIGNMENT);
        this.pBar.setOpaque(false);
        content.add(this.pBar, BorderLayout.CENTER);
        
        //set progress text
        this.progressText.setText("Iniciando aplicacion");
        this.progressText.setAlignmentX(CENTER_ALIGNMENT);
        content.add( this.progressText,BorderLayout.CENTER );
        
        
	}
	
	public void updateProgress(int n, String progressText, int milliseconds){
		
		this.setProgressText(progressText);
		this.setProgressValue(n);
	}
	
	public void setDelayPerProcess(int milliseconds){
		this.delay = milliseconds;
	}
	
	private void setProgressText(String progressText) {
		this.progressText.setText(progressText);
		try { Thread.sleep(this.delay); } catch (Exception e) {}
	}
	
	private void setProgressValue( int n ){
		for( int i = this.pBar.getValue(); i <= n; i++ ){
			this.pBar.setValue(i);
			try { Thread.sleep(40); } catch (Exception e) {}
		}
	}
	
	public void showSplashScreen(boolean state){
		this.setVisible( state & dontShowSplashScreen );
	}
	
	public void finishSplashScreen() {
		
		
	}

	private String promptForFile(){
		  JFileChooser fc=new JFileChooser();
		  int returnVal=fc.showOpenDialog(this);
		  if (returnVal == JFileChooser.APPROVE_OPTION) {
		    return fc.getSelectedFile().getAbsolutePath();
		  }
		 else {
		    return null;
		  }
		}
	
}
